import requests
import parsel
import os




for page in range(1,17):
    # print(f'================================正在爬取{page}页内容=====================================')
    link = f'https://www.3gbizhi.com/meinv/mnxz_{page}.html'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
    }
    resp_2 = requests.get(url=link, headers=headers)
    selector_1 = parsel.Selector(resp_2.text)
    href_list = selector_1.css('.contlistw .cl li a::attr(href)').getall()
    n = selector_1.css('.contlistw .cl li a::attr(title)').getall()

    for url in href_list:
        # url = 'https://www.3gbizhi.com/meinv/mnxz.html'
        # url = 'https://www.3gbizhi.com/meinv/mn1869.html'
        # 'https://www.3gbizhi.com/meinv/mn1868.html'

        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
        }

        resp = requests.get(url=url, headers=headers)
        # print(resp.text)
        selector = parsel.Selector(resp.text)
        # 提取图片标题
        title = selector.css('.showcontw #showimg img::attr(alt)').get()
        img_url_list = selector.css('.piclistw li a img::attr(src)').getall()

        # 创建文件路径
        file = 'img\\'
        # 判断这个文件是否存在
        if not os.path.exists(file):
            # 自动创建文件夹
            os.mkdir(file)

        num = 1

        for img_url in img_url_list:
            img_url = img_url.replace('thumb_200_0_', '')
            name = img_url.split('/')[-1]

            # 保存数据
            resp_1 = requests.get(url=img_url, headers=headers).content
            # with open(f'img/{title}/{name}', mode='wb')as f:
            with open(file + title + str(num) + '.jpg', mode='wb') as f:
                f.write(resp_1)
            print(f'{img_url}正在下载')
            num += 1
#     print(f'================================{page}页内容爬取完成=====================================')
#
# print('全部爬取完成！！！！！！！！！！！！！！！！！！！')